package lte

import (
	"sort"

	"gitee.com/thubcc/mobile/utils"
)

type DlRxFrame struct {
	frame   []complex128
	symbols utils.Array3
	Ch      [2]*ChestDL
}

func (dlrx *DlRxFrame) best() (idx int, amp float64) {
	if dlrx.Ch[0].Amp > dlrx.Ch[1].Amp {
		return dlrx.Ch[0].Peak, dlrx.Ch[0].Amp
	}
	return dlrx.Ch[1].Peak, dlrx.Ch[1].Amp
}

func NewDlRxFrame(frame []complex128, cid []int) (rs DlRxFrames) {
	symbols := ofdm_rx(frame, 6)
	for _, id := range cid {
		r := &DlRxFrame{
			frame:   frame,
			symbols: symbols,
			Ch: [2]*ChestDL{
				ChannelEstimateDL(symbols, id, 0),
				ChannelEstimateDL(symbols, id, 1),
			},
		}
		rs = append(rs, r)
	}
	sort.Sort(rs)
	return rs
}

type DlRxFrames []*DlRxFrame

func (rs DlRxFrames) Len() int      { return len(rs) }
func (rs DlRxFrames) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
func (rs DlRxFrames) Less(i, j int) bool {
	_, ai := rs[i].best()
	_, aj := rs[j].best()
	return ai > aj
}
